package cre.sample.test.gc.oom;

import java.util.ArrayList;
import java.util.List;

/**
 * jdk1.6 下，会报错OutOfMemoryError:PermGen space，因为jdk1.6，把方法区认为是永久代了
 * <p/>
 * VM Args: -XX:PermSize=10M -XX:MaxPermSize=10M
 * Created by CreGu on 2016/5/24.
 */
public class RuntimeConstantPoolOOMJdk6 {

    public static void main(String[] args) {
// jdk6 inern 返回都是永久代中实例的引用

        String s = "abc";

        String str1 = new StringBuilder("计算机1").append("软件").toString();
        System.out.println("str1.intern():"+str1.intern().hashCode());
        System.out.println("str1:"+str1.hashCode());
        System.out.println(str1.intern() == (str1));

        String str2 = new StringBuilder("ja").append("va").toString();
        System.out.println("str2.intern():"+str2.intern().hashCode());
        System.out.println("str2:"+str2.hashCode());
        System.out.println(str2.intern() == (str2));

//        使用list保持着常量池的引用，避免Full GC回收常量池
        List<String> strings = new ArrayList<String>();
        int i = 0;
        while (true) {
//            String.intern()，native方法，会在常量池添加String变量
            strings.add(String.valueOf(i++).intern());
        }
    }
}
